package com.moonciki.artadmin.web.service.student.impl;

import com.moonciki.artadmin.core.base.BaseServiceImpl;
import com.moonciki.artadmin.core.enums.FileTypeEnum;
import com.moonciki.artadmin.core.exception.CustomException;
import com.moonciki.artadmin.core.model.FileWrapper;
import com.moonciki.artadmin.core.model.PageData;
import com.moonciki.artadmin.core.model.PageParameter;
import com.moonciki.artadmin.core.tools.CustomPageHelper;
import com.moonciki.artadmin.core.utils.excel.ExcelUtils;
import com.moonciki.artadmin.core.utils.excel.ExcelWriter;

import com.moonciki.artadmin.web.mybatis.entity.fuzhu.FzStudent;
import com.moonciki.artadmin.web.mybatis.mapper.fuzhu.FzStudentMapper;
import com.moonciki.artadmin.web.service.student.IFzStudentService;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import org.springframework.transaction.annotation.Transactional;
import com.moonciki.artadmin.web.mybatis.entity.fuzhu.FzStudentScore;
import com.moonciki.artadmin.web.mybatis.mapper.fuzhu.FzStudentScoreMapper;

import java.util.Date;
import java.util.List;

/**
 * 学生Service业务层处理
 * @author ruoyi
 * @date 2022-02-16
 */
@Service
public class FzStudentServiceImpl extends BaseServiceImpl implements IFzStudentService {

    @Autowired
    private FzStudentMapper fzStudentMapper;

    @Autowired
    private FzStudentScoreMapper fzStudentScoreMapper;

    @Override
    public FzStudent getById(Long id) {
        FzStudent fzStudent = fzStudentMapper.selectByPrimaryKey(id);
        Example subExa = new Example(FzStudentScore.class);
        subExa.and().andEqualTo("studentId", id);
        List<FzStudentScore> subList = fzStudentScoreMapper.selectByExample(subExa);
        fzStudent.setFzStudentScoreList(subList);
        return fzStudent;
    }

    @Override
    public PageData<FzStudent> getPageList(PageParameter pageParameter, FzStudent fzStudent) {
        CustomPageHelper<FzStudent> sqlPager = pageParameter.startPagger();
        List<FzStudent> select = fzStudentMapper.select(fzStudent);
        PageData<FzStudent> pageData = sqlPager.endPagger(select);
        return pageData;
    }

    @Override
    public List<FzStudent> getList(FzStudent fzStudent) {
        List<FzStudent> select = fzStudentMapper.select(fzStudent);
        return select;
    }

    @Override
    public FileWrapper export(FzStudent fzStudent) {
        FileWrapper fileWrapper = new FileWrapper();

        List<FzStudent> list = this.getList(fzStudent);

        String sheetName = "学生数据";
        String fileName = sheetName + FileTypeEnum.xlsx.getSuffix();

        String[] titleArray = {
            "id", "姓名", "年龄（1,2）", "ip", "创建时间"
        };
        String[] fieldArray = {
            "id", "name", "age", "ipAddress", "createTime"
        };

        ExcelWriter excelWriter = null;

        try {
            excelWriter = ExcelWriter.getExcelWriter(fileName, 0, sheetName);
            excelWriter.writeTitleData(titleArray);

            if(CollectionUtils.isNotEmpty(list)){
                for(FzStudent oneVo: list){
                    Object[] rowData = ExcelUtils.objToExcelArray(oneVo, fieldArray);

                    excelWriter.writeData(rowData);
                }
            }

            byte[] fileBytes = excelWriter.getBytes();

            fileWrapper.setDataBytes(fileBytes);
            fileWrapper.setFileName(fileName);
        } catch (Exception e) {
            throw CustomException.createException(e);
        } finally {
            if (excelWriter != null) {
                try {
                    excelWriter.close();
                } catch (Exception e) {
                    throw CustomException.createException(e);
                }
            }
        }
        return fileWrapper;
    }

    @Transactional
    @Override
    public FzStudent insertFzStudent(FzStudent fzStudent) {
        fzStudent.setCreateTime(new Date());

        int i = fzStudentMapper.insertSelective(fzStudent);
        this.insertFzStudentScore(fzStudent);
        return fzStudent;
    }

    @Transactional
    @Override
    public int updateFzStudent(FzStudent fzStudent) {
        Long id = fzStudent.getId();

        this.deleteById(id);
        this.insertFzStudentScore(fzStudent);
        return fzStudentMapper.updateByPrimaryKeySelective(fzStudent);
    }

    @Transactional
    @Override
    public int deleteByIds(List<Long> idList) {

        Example subExa = new Example(FzStudentScore.class);
        subExa.and().andIn("studentId", idList);
        fzStudentScoreMapper.deleteByExample(subExa);
        Example exa = new Example(FzStudent.class);
        exa.and().andIn("id", idList);
        int i = fzStudentMapper.deleteByExample(exa);
        return i;
    }

    @Transactional
    @Override
    public int deleteById(Long id) {
        Example subExa = new Example(FzStudentScore.class);
        subExa.and().andEqualTo("studentId", id);
        fzStudentScoreMapper.deleteByExample(subExa);
        int i = fzStudentMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 新增分数信息
     * @param fzStudent 学生对象
     */
    public void insertFzStudentScore(FzStudent fzStudent) {
        List<FzStudentScore> fzStudentScoreList = fzStudent.getFzStudentScoreList();
        Long id = fzStudent.getId();
        if (CollectionUtils.isNotEmpty(fzStudentScoreList)){
            for (FzStudentScore fzStudentScore : fzStudentScoreList) {
                fzStudentScore.setStudentId(id);
            }
            fzStudentScoreMapper.insertList(fzStudentScoreList);
        }
    }
}
